/*
 * Created on Dec 19, 2003
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */

/**
 * @author msoong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */

package ModuleBuilder;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import ModuleBuilder.UI.*;
import ModuleBuilder.UI.tree.*;
import ModuleBuilder.model.*;
import ModuleBuilder.control.*;

public class ModuleBuilderMain extends JFrame {

	JPanel contentPane;
	JMenuBar jMenuBar1 = new JMenuBar();
	JMenu jMenuFile = new JMenu();
	JMenuItem jMenuFileExit = new JMenuItem(); 
	JMenuItem  jMenuFileOpen = new JMenuItem();
	JMenuItem jMenuFileSave = new JMenuItem();
	
	// MapBuilder mapBuilder;
	BuilderManager manager;
	
	private static  ModuleBuilderMain instance;
	 
	public ModuleBuilderMain()  {
		super("CyberWar Module Builder");
		setSize(800,600);
		
		// setup windows look and feel
		try {
			  UIManager.setLookAndFeel(
				// UIManager.getCrossPlatformLookAndFeelClassName());
				UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) { 
		}
		
		Container contents = getContentPane();
		JTabbedPane jtp = new JTabbedPane();
		manager = new BuilderManager();
		 
		PictureBuilder figTop= new PictureBuilder(manager, null);
		jtp.addTab("Figure Top View", figTop );
		PictureBuilder figSide= new PictureBuilder(manager, new Dimension(240,180));
		jtp.addTab("Figure Side View", figSide );
		PictureBuilder terrainTop= new PictureBuilder(manager, null);
		jtp.addTab("Terrain", terrainTop);
		GameParameter gameParm = new GameParameter(manager);
		jtp.addTab("Game Parameter",  gameParm);
		MapBuilder mapBuilder = new MapBuilder(manager); 
		jtp.addTab("Map", mapBuilder);
		GameTreeEditor obTree = new GameTreeEditor();
		jtp.addTab("OB Editor", obTree );
		contents.add(jtp);
		
		// create a menu bar
		JMenuBar bar = new JMenuBar();
		JMenu menu = new JMenu("File");

		//jMenuBar1.add(jMenuFile);
		//this.setJMenuBar(jMenuBar1);
		jMenuFileOpen.setText("Open");
		jMenuFileOpen.addActionListener(new ActionListener()  {
			public void actionPerformed(ActionEvent e) {
			  jMenuFileOpen_actionPerformed(e);
			}
		});
		
		jMenuFileSave.setText("Save");
		jMenuFileSave.addActionListener(new ActionListener()  {
			public void actionPerformed(ActionEvent e) {
			  jMenuFileSave_actionPerformed(e);
			}
		});
		
		//jMenuFile.setText("File");
		jMenuFileExit.setText("Exit");
		jMenuFileExit.addActionListener(new ActionListener()  {
		  public void actionPerformed(ActionEvent e) {
			jMenuFileExit_actionPerformed(e);
		  }
		});

		bar.add(menu);

		menu.add(jMenuFileOpen);
		menu.add(jMenuFileSave);
		menu.add(jMenuFileExit);
		JRootPane root = this.getRootPane();
		root.setJMenuBar(bar);
		
		// now hand all these into the BuilderManger
		manager.setFigureTopBuilder(figTop);
		manager.setFigureSideBuilder(figSide);
		manager.setGameParameter(gameParm);
		manager.setMapTileBuilder( terrainTop);
		manager.setMapBuilder(mapBuilder);
		manager.setGameTreeEditor(obTree);
		
		validate();
		setVisible( true );
		instance = this;
	}
	
	public static ModuleBuilderMain getInstance() {
		return instance;
	}
	
/*	public static void main(String[] args) {
		new ModuleBuilderMain();
		
	}
	
*/	/**Overridden so we can exit when window is closed*/
	protected void processWindowEvent(WindowEvent e) {
	  super.processWindowEvent(e);
	  if (e.getID() == WindowEvent.WINDOW_CLOSING) {
		jMenuFileExit_actionPerformed(null);
	  }
	}
	
	/**File | Exit action performed*/
	public void jMenuFileExit_actionPerformed(ActionEvent e) {
	  System.exit(0);
	}

	public void jMenuFileOpen_actionPerformed(ActionEvent e) {
		Loader.load(manager, this);
	}
	
	public void jMenuFileSave_actionPerformed( ActionEvent e) {
		Storer.getScenarioModel(manager, this);
	}
}
